//
//  BKXNetworkTools.swift
//  微博
//
//  Created by 田凯 on 16/4/19.
//  Copyright © 2016年 田凯. All rights reserved.
//

import UIKit
import AFNetworking
//定义方法的枚举
enum RequestMethed: Int{
    case GET = 0
    case POST = 1
}

class BKXNetworkTools: AFHTTPSessionManager {
    
    //设置全局访问点
    static let sharedTools: BKXNetworkTools = {
        let  tools = BKXNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        
        
        }()
    
    // MARK: - 网络请求方法
    func request(methed: RequestMethed, urlString: String, paramerent: AnyObject?, callbag:(reponce:AnyObject?,error: NSError?)->()){
    
        if methed == .GET{
        self.GET(urlString, parameters: paramerent, progress: nil, success: { (_, reponce) -> Void in
            callbag(reponce: reponce, error: nil)
            }, failure: { (_, error) -> Void in
                callbag(reponce: nil, error: error)

        })
        
        }else{
    self.POST(urlString, parameters: paramerent, progress: nil, success: { (_,reponce ) -> Void in
        callbag(reponce: reponce, error: nil)

        }, failure: { (_, error) -> Void in
        callbag(reponce: nil, error: error)

    })
        
        

}
    
    }
    

}

/// 请求微博首页的方法
extension BKXNetworkTools{
    func loadStatus(accessToken: String, since_id: Int64, max_id: Int64,callbag:(reponce:AnyObject?,error: NSError?)->()){
        // 请求url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        ccess_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
//        since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
//        max_id
        let parament = [
            "access_token": accessToken,
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"
            
        ]
        
        // 发送网络请求
        request(.GET, urlString: urlString, paramerent: parament) { (reponce, error) -> () in
            callbag(reponce: reponce, error: error)
        }
        
    
    }


}














